package api;

import com.alibaba.fastjson.JSON;
import domain.Notice;
import service.NoticeService;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;

public class GetNoticeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        NoticeService noticeService=new NoticeService();
        Response build= null;
        Notice notice = null;
        try {
            notice=noticeService.getRecentNotice();
            build=Response.build(ResponseCode.SUCCESS,notice);

        }catch (SQLException e){
            e.printStackTrace();
            build=Response.build(ResponseCode.ERROR_DB);
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());
    }

}
